<?php
namespace MyAdmin\Model;

use Think\Model;

/**
 * TODO
 * 废弃
 */
class AdminsModel extends Model
{
  protected $autoCheckFields =false;

  public function set_admin_session($info)
  {
    $_SESSION["admin_id"] = $info['id'];
    $_SESSION["phone"] = $info['phone'];
    $_SESSION["token"] = $info['token'];
    $_SESSION["name"] = $info['name'];
  }
  //退出
  public function logout_admins()
  {
    session_unset();
    session_destroy();
    setcookie('uautoadmin', '', (gmtime()-C('AUTO_LOGIN_TIME')*3600*24), C('cookie_pe'));
  }
  //获取信息
  public function get_admininfo($admin_id)
  {
    $info = M("Admins")->where("status=1 and id=".$admin_id)->find();
    return $info;
  }
  public function get_appinfo($app_id)
  {
    $info = M("Apps")->where("id=$app_id")->find();
    return $info;
  }
  public function get_app_upyun_config($app_info)
  {
    $config = array(
      // 又拍云服务器，根据自己的实际情况，选择一个即可
      // v0.api.upyun.com(自动判断)  v1.api.upyun.com(电信)
      // v2.api.upyun.com(联通网通)  v3.api.upyun.com (移动铁通)
      'host' => $app_info['upyun_host'],
      'bucket' => $app_info['upyun_bucket'],
      'username' => $app_info['upyun_username'],
      'password' => $app_info['upyun_password'],
      'endpoint' => $app_info['upyun_endpoint']
    );
    return $config;
  }
  //创建
  public function create_token($admin_id)
  {
    $now_time = gmtime();
    $i=1;
    while ($i<2) {
      $token=random(28);
      $result=M("Admins")->where("token='".$token."'")->getField("id");
      if (!$result) {
        if ($admin_id) {
          $update_data['token']=$token;
          $update_data['expire_time']=$now_time+7*3600*24;//暂时不利用过期时间==先记录7天
          M("Admins")->where("id='".$admin_id."'")->save($update_data);
        }

        $i = 2;
      }
    }

    return $token;
    // $token = md5($user_id.randomFromDev(16));
    // $data['token'] = $token;
    // $result = M("users")->where("id=$user_id")->save($data);
    // if ($result) {
    //     return $token;
    // }

    // return false;
  }
}
